<?php

namespace app\models\Child;

use Yii;

/**
 * This is the model class for table "t_booking_info".
 *
 * @property integer $booking_id
 * @property string $booking_no
 * @property integer $user_id
 * @property integer $doctor_id
 * @property integer $subclinic_id
 * @property string $user_name
 * @property integer $user_gender
 * @property string $user_phone
 * @property integer $is_first
 * @property integer $booking_status
 * @property string $booking_time
 * @property string $create_time
 *
 * @property TUserInfo $user
 * @property TSubclinicInfo $subclinic
 */
class TBookingInfo extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 't_booking_info';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_id', 'doctor_id', 'subclinic_id', 'user_gender', 'is_first', 'booking_status','cancel_type','patient_id','booking_type'], 'integer'],
            [['booking_time', 'create_time','cancel_time'], 'safe'],
            [['booking_no'], 'string', 'max' => 32],
            [['user_name'], 'string', 'max' => 50],
            [['user_phone'], 'string', 'max' => 11],
            [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => TUserInfo::className(), 'targetAttribute' => ['user_id' => 'user_id']],
            [['subclinic_id'], 'exist', 'skipOnError' => true, 'targetClass' => TSubclinicInfo::className(), 'targetAttribute' => ['subclinic_id' => 'subclinic_id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'booking_id' => 'Booking ID',
            'booking_no' => 'Booking No',
            'patient_id' => 'Patient Id',
            'booking_type' => 'Booking Type',
            'user_id' => 'User ID',
            'doctor_id' => 'Doctor ID',
            'subclinic_id' => 'Subclinic ID',
            'user_name' => 'User Name',
            'user_gender' => 'User Gender',
            'user_phone' => 'User Phone',
            'is_first' => 'Is First',
            'booking_status' => 'Booking Status',
            'booking_time' => 'Booking Time',
            'create_time' => 'Create Time',
            'cancel_time' => 'Cancel Time',
            'cancel_type' => 'Cancel Type',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(TUserInfo::className(), ['user_id' => 'user_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getSubclinic()
    {
        return $this->hasOne(TSubclinicInfo::className(), ['subclinic_id' => 'subclinic_id']);
    }
    /*
    * 预约列表
    */
    public function getBookingInfo($patient_id)
    {
        $list = $this->find()
            ->where(['patient_id' => $patient_id])
            ->orderBy('booking_id','DESC')
            ->asArray()
            ->one();
        return $list;
    }
    /*
     * 预约列表
     */
    public function getBookingList($user_id)
    {
        $list = $this->find()
                ->select('t_order_info.order_id,t_booking_info.booking_id,booking_no,subclinic_name,sys_user_info.user_name,booking_time,booking_status,is_reply')
                ->join('left join','t_order_info','t_order_info.booking_id = t_booking_info.booking_id')
                ->join('left join','t_subclinic_info','t_subclinic_info.subclinic_id = t_booking_info.subclinic_id')
                ->join('left join','sys_user_info','sys_user_info.user_id = t_booking_info.doctor_id')
                ->where(['t_booking_info.user_id' => $user_id])
                ->asArray()
                ->all();
        return $list;
    }
    
    /*
     * 患者预约
     */
    public function setBooking($booking_no,$subclinic_id,$user_id,$user_name,$user_gender,$user_phone,$doctor_id,$booking_time,$create_time)
    {
        $this->booking_no = $booking_no;
        $this->subclinic_id = $subclinic_id;
        $this->user_id = $user_id;
        $this->user_name = $user_name;
        $this->user_gender = $user_gender;
        $this->user_phone = $user_phone;
        $this->doctor_id = $doctor_id;
        $this->booking_time = $booking_time;
        $this->create_time = $create_time;
        
        return $this->save();
    }
    
    /*
     * 获取医生已预约时间
     */
    public function getTimes($doctor_id,$start_time,$end_time)
    {
        $list = $this->find()
                ->select('booking_id,user_name,booking_time,booking_status')
                ->where(['doctor_id' => $doctor_id])
                ->andWhere(['and','unix_timestamp(booking_time) > '.$start_time,'unix_timestamp(booking_time) < '.$end_time])
                ->asArray()
                ->all();
        return $list;
    }
    
    /*
     * 取消预约
     */
    public function removeBooking($booking_id,$cancel_time)
    {
        $booking = $this->find()
                    ->where(['booking_id' => $booking_id])
                    ->one();
        $booking->booking_status = 5;
	$booking->cancel_time = $cancel_time;
	$booking->cancel_type = 1;
        return $booking->save();
    }
    
    /*
     * 判断预约的医生在该时间段是否已被预约
     */
    public function getList($doctor_id,$booking_time)
    {
        $list = $this->find()
                ->where(['doctor_id' => $doctor_id,'booking_time' => $booking_time])
                ->asArray()
                ->one();
        return $list;
    }
    
    public function getLists($user_id,$booking_time)
    {
        $lists = $this->find()
                ->where(['user_id' => $user_id,'booking_time' => $booking_time])
                ->asArray()
                ->one();
        return $lists;
    }
    
}
